home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Color OneScanner SDK / Scan Image 1.0 / Source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-24  |  11.5 KB  |  503 lines  |  [TEXT/MPCC]

  1. /*************************************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Windows.h>
  29.  
  30. #include "App.h"
  31. #include "Proto.h"
  32.  
  33.  
  34. //----------------------------------------------------------------------
  35. //    Globals
  36. //----------------------------------------------------------------------
  37.  
  38. extern Boolean        gHasAbout;        // have an about box?
  39. extern short        gWindCount;
  40.  
  41.  
  42. //----------------------------------------------------------------------
  43. //
  44. //    CreateWindow - create a window from the info passed in. Will try to 
  45. //                   load from resource if resID is supplied.
  46. //
  47. //----------------------------------------------------------------------
  48.  
  49. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  50.                         Boolean visible, short procID , short kind,
  51.                         WindowRef behind, Boolean goAwayFlag, long refCon)
  52. {
  53.     OSErr            err = nil;
  54.     WindowRef        newWindow = nil;
  55.     Boolean            isFloater;
  56.     
  57.     
  58.     if (resID != nil)         // if res id isn't nil then load from disk
  59.         newWindow = GetNewCWindow(resID, wStorage, (WindowRef) -1);
  60.     else                    // otherwise make a new windowRecord
  61.          newWindow = NewCWindow(wStorage, bounds, title, visible, 
  62.                                procID, (WindowRef) -1, goAwayFlag, refCon);
  63.                                
  64.     if (newWindow != nil) 
  65.     {
  66.         NewWindowTitle(newWindow, title);
  67.         err = InitWindowProcs(newWindow, kind);
  68.         
  69.         if (err == noErr) 
  70.         {
  71.             SetPort(newWindow);
  72.             ShowWindow(newWindow);
  73.         }
  74.                 
  75.         // initialization of Document Record faild
  76.         // so kill the return window    
  77.         else 
  78.         {
  79.             newWindow = nil;
  80.             HandleError(err, false);
  81.         }        
  82.     }
  83.     
  84.     return newWindow;
  85.  
  86. }
  87.     
  88.  
  89. //----------------------------------------------------------------------
  90. //
  91. //    RemoveWindow - applications Doc window disposal routine.
  92. //                 
  93. //
  94. //----------------------------------------------------------------------
  95.  
  96. OSErr RemoveWindow( WindowRef window )
  97. {
  98.     OSErr            err = nil;
  99.     short            kind;
  100.     DocHnd            doc;
  101.     
  102.     
  103.     kind = GetWindKind( window );
  104.     if ( kind < kDocKind || kind > kAboutKind )
  105.         return -1; // not our window
  106.     
  107.     doc = (DocHnd)GetWRefCon( window );
  108.     if ( doc != nil ) {
  109.         switch( kind ) {
  110.             case kDocKind:
  111.                 DisposeGWorld((**doc).world);
  112.                 DisposeHandle( (Handle)doc );
  113.                 DisposeWindow( window );
  114.                 err = noErr;
  115.                 break;
  116.             
  117.             case kAboutKind:
  118.                 DisposeHandle( (Handle)doc );
  119.                 DisposeWindow( window );
  120.                 err = noErr;
  121.                 gHasAbout = false;    // allow new about box
  122.                 break;
  123.             
  124.             default:
  125.                 break;
  126.                     
  127.         
  128.         }
  129.         
  130.         window = FrontWindow();
  131.         if (window != nil)
  132.             ActivateWindow(window);
  133.             
  134.     }
  135.     
  136.     return err;
  137. }        
  138.  
  139.  
  140. //----------------------------------------------------------------------
  141. //
  142. //    NewWindowTitle - if supplied title is nil then title is set to 
  143. //                      global "gWindCount".
  144. //
  145. //----------------------------------------------------------------------
  146.  
  147. void NewWindowTitle(WindowRef window, Str255 str)
  148. {
  149.     Str255        catStr = "\pUntitled ";
  150.     Str255        newStr;
  151.     
  152.     
  153.     if (str == nil || StrLength(str) == 0) {
  154.         pstrcpy(newStr, catStr);
  155.         NumToString(gWindCount, catStr);
  156.         pstrcat(newStr, catStr);
  157.         gWindCount++;
  158.         
  159.         SetWTitle(window,newStr);
  160.     }
  161.     else
  162.         SetWTitle(window, str);
  163.  
  164. }
  165.  
  166.  
  167. //----------------------------------------------------------------------
  168. //
  169. //    InitWindowProcs - init a window with proper callback event. fills 
  170. //                       out custom procs for different windowkinds.
  171. //
  172. //----------------------------------------------------------------------
  173.  
  174. OSErr InitWindowProcs(WindowRef window, short windKind)
  175. {
  176.     OSErr            err = nil;
  177.     DocHnd            doc;
  178.     
  179.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  180.     if (doc != nil) {
  181.         SetWRefCon(window, (long)doc);
  182.  
  183.         switch(windKind) {
  184.             case kDocKind:
  185.                 (**doc).idleProc        = DoIdle;
  186.                 (**doc).mMenuProc        = HandleMenuChoice;
  187.                 (**doc).inContentProc    = HandleContentClick;
  188.                 (**doc).inGoAwayProc    = nil;
  189.                 (**doc).inZoomProc        = HandleZoomClick;
  190.                 (**doc).inGrowProc        = HandleGrow;
  191.                 (**doc).keyProc            = nil;
  192.                 (**doc).activateProc    = DoActivate;
  193.                 (**doc).updateProc        = DrawWindow;    
  194.                 (**doc).hScroll         = nil;
  195.                 (**doc).vScroll            = nil;
  196.                 (**doc).world            = nil;
  197.                 (**doc).pict            = nil;
  198.                 (**doc).fileType        = nil;
  199.                 (**doc).printer            = nil;
  200.                 (**doc).dirty            = false;
  201.                 
  202.                 (**doc).fileType        = kPICTType;
  203.                 
  204.                 InstallScrollBars(window, doc);
  205.                 break;
  206.                 
  207.             case kDialogKind:
  208.                 break;
  209.  
  210.  
  211.             case kAboutKind:
  212.                 (**doc).idleProc        = DoIdle;
  213.                 (**doc).mMenuProc        = HandleMenuChoice;
  214.                 (**doc).inContentProc    = nil;
  215.                 (**doc).inGoAwayProc    = nil;
  216.                 (**doc).inZoomProc        = nil;
  217.                 (**doc).inGrowProc        = nil;
  218.                 (**doc).keyProc            = nil;
  219.                 (**doc).activateProc    = nil;
  220.                 (**doc).updateProc        = DrawAboutWindow;    
  221.                 (**doc).hScroll         = nil;
  222.                 (**doc).vScroll            = nil;
  223.                 (**doc).world            = nil;
  224.                 (**doc).pict            = GetPicture(rAboutPictID);
  225.                 (**doc).fileType        = nil;
  226.                 (**doc).printer            = nil;
  227.                 (**doc).dirty            = false;
  228.                 break;
  229.                 
  230.             default:
  231.                 err = paramErr;
  232.                 break;    
  233.         }
  234.         ((WindowPeek)window)->windowKind = windKind;
  235.  
  236.     }            
  237.     return err;
  238.  
  239. }
  240.  
  241.         
  242. //----------------------------------------------------------------------
  243. //
  244. //    DrawWindow - custom proc that is called to update window contents.
  245. //                 
  246. //
  247. //----------------------------------------------------------------------
  248.  
  249. void DrawWindow(WindowRef window, void *refCon)
  250. {
  251.     DocHnd            doc;
  252.     GWorldPtr        theWorld;
  253.     PixMapHandle    thePix;
  254.     Rect            cRect;
  255.     Rect            bounds;
  256.     
  257.     doc = (DocHnd)GetWRefCon(window);    
  258.     if (doc != nil) {
  259.         SetPort(window);
  260.         GetContRect(window, &cRect);
  261.         ClipRect(&cRect);
  262.         
  263.         theWorld = (**doc).world;
  264.  
  265.         if (theWorld != nil ) {
  266.             bounds = theWorld->portRect;
  267.             OffsetRect(&bounds, -GetCtlValue((**doc).hScroll), 
  268.                        -GetCtlValue((**doc).vScroll));
  269.             thePix = GetGWorldPixMap(theWorld);
  270.             if (LockPixels(thePix)) {
  271.                 CopyBits((BitMap *) *thePix, &window->portBits,
  272.                          &theWorld->portRect, &bounds, srcCopy, nil);
  273.                 
  274.                 UnlockPixels(thePix);
  275.             }             
  276.                         
  277.         }
  278.         else
  279.             EraseRect(&cRect);    
  280.  
  281.         ClipRect(&window->portRect);
  282.         DrawGrowIcon(window);
  283.         UpdateControls(window, window->visRgn);
  284.     }
  285.  
  286. }
  287.  
  288.  
  289.  
  290. //----------------------------------------------------------------------
  291. //
  292. //    DrawAboutWindow - custom proc that is called to update about window.
  293. //                 
  294. //
  295. //----------------------------------------------------------------------
  296.  
  297. void DrawAboutWindow( WindowRef window, void *refCon )
  298. {    
  299.     DocHnd        doc;
  300.     
  301.     doc = (DocHnd)GetWRefCon(window);    
  302.     if ( (**doc).pict != nil ) 
  303.         DrawPicture( (**doc).pict, &window->portRect );
  304.  
  305. }
  306.  
  307.  
  308. //----------------------------------------------------------------------
  309. //
  310. //    DoResizeWindow - custom proc that is called to update window.
  311. //                 
  312. //
  313. //----------------------------------------------------------------------
  314.  
  315. void DoResizeWindow (WindowRef window) 
  316. {
  317.     DocHnd            doc;
  318.     ControlHandle    hCtl, vCtl;
  319.     RgnHandle        tempRgn;
  320.     RgnHandle        oldCtlRgn;
  321.     short            max, oldCntlVal;
  322.     short            pV, wV;
  323.     short             pH, wH;
  324.     Rect            hSRect;
  325.     Rect            newDocRect;
  326.     Rect            pictRect;
  327.     Rect            paneRect;
  328.  
  329.     doc = (DocHnd)GetWRefCon(window);
  330.     if (doc != nil) {
  331.         pictRect = (**doc).world->portRect;
  332.         newDocRect = (**window->visRgn).rgnBBox;
  333.  
  334.         hCtl = (**doc).hScroll;
  335.         vCtl = (**doc).vScroll;
  336.         
  337.         hSRect = (**hCtl).contrlRect;
  338.         hSRect.right += kScrollWidth;
  339.         ClipRect(&window->portRect);
  340.         RectRgn(oldCtlRgn = NewRgn(), &hSRect);
  341.         RectRgn(tempRgn = NewRgn(), &(**vCtl).contrlRect);
  342.         UnionRgn(oldCtlRgn, tempRgn, oldCtlRgn);
  343.         EraseRgn(oldCtlRgn);
  344.         
  345.         (**hCtl).contrlVis = 0;
  346.         (**vCtl).contrlVis = 0;
  347.  
  348.         paneRect = newDocRect;
  349.         paneRect.bottom -= kScrollWidth;
  350.         paneRect.right -= kScrollWidth;
  351.         
  352.         MoveControl(hCtl, paneRect.left - 1, paneRect.bottom);    
  353.         MoveControl(vCtl, paneRect.right, paneRect.top - 1);
  354.             
  355.         SizeControl(hCtl, 2+paneRect.right - paneRect.left, kScrollWidth + 1);    
  356.         SizeControl(vCtl, kScrollWidth + 1, 2+paneRect.bottom - paneRect.top);
  357.         
  358.         (**hCtl).contrlVis = 255;
  359.         (**vCtl).contrlVis = 255;
  360.         
  361.         pV = pictRect.bottom - pictRect.top;
  362.         wV = paneRect.bottom - paneRect.top;
  363.         if (wV >= pV)
  364.             max = 0;
  365.         else
  366.             max = ((MAX (0, pV - wV)) / kScrollDelta) + 1;
  367.             
  368.         oldCntlVal = GetControlValue(vCtl);
  369.         SetControlMinimum(vCtl, 0);
  370.         SetControlMaximum(vCtl, max);
  371.         SetControlValue(vCtl, oldCntlVal );
  372.         
  373.         HiliteControl(vCtl, (max == 0) ? (255):(0));    
  374.  
  375.         pH = pictRect.right - pictRect.left;
  376.         wH = paneRect.right - paneRect.left;
  377.         if (wH >= pH)
  378.             max = 0 ;
  379.         else
  380.             max = ((MAX (0, pH - wH )) / kScrollDelta) +1;
  381.         oldCntlVal = GetControlValue(hCtl);
  382.         SetControlMinimum(hCtl, 0);
  383.         SetControlMaximum(hCtl, max) ;
  384.         SetControlValue(hCtl, oldCntlVal);
  385.         
  386.         HiliteControl(hCtl, (max == 0) ? (255):(0));    
  387.                 
  388.         DisposeRgn(oldCtlRgn);
  389.         DisposeRgn(tempRgn);
  390.         AdjustScrollValues(window);
  391.         InvalRect(&window->portRect);
  392.  
  393.     }
  394.  
  395. }
  396.  
  397.  
  398.  
  399. //----------------------------------------------------------------------
  400. //
  401. //    ActivateWindow - 
  402. //                 
  403. //
  404. //----------------------------------------------------------------------
  405.  
  406. void ActivateWindow(WindowRef window)
  407. {
  408.     Boolean            active = true;
  409.     
  410.     
  411.     HiliteWindow(window, true);
  412.     CustomWindowEvent(kActivateProc, window, &active);
  413.  
  414. }
  415.  
  416. //----------------------------------------------------------------------
  417. //
  418. //    DeactivateWindow - 
  419. //                 
  420. //
  421. //----------------------------------------------------------------------
  422.  
  423. void DeactivateWindow(WindowRef window)
  424. {
  425.     Boolean            active = false;
  426.     
  427.     
  428.     HiliteWindow(window, false);
  429.     CustomWindowEvent(kActivateProc, window, &active);
  430.  
  431. }
  432.  
  433.  
  434.  
  435. //----------------------------------------------------------------------
  436. //
  437. //    GetWindKind - returns the windowkind.
  438. //                 
  439. //
  440. //----------------------------------------------------------------------
  441.  
  442. short GetWindKind(WindowRef window)
  443. {
  444.  
  445.     return ((WindowPeek)window)->windowKind;
  446.  
  447. }
  448.  
  449.  
  450. //----------------------------------------------------------------------
  451. //
  452. //    GetIsAppWindow - is the window a 'userKind'.
  453. //                 
  454. //
  455. //----------------------------------------------------------------------
  456.  
  457. Boolean GetIsAppWindow(WindowRef window)
  458. {
  459.     return (GetWindKind(window) == kDocKind);
  460.  
  461. }
  462.  
  463.  
  464. //----------------------------------------------------------------------
  465. //
  466. //    GetIsAboutWindow - is the window an about box.
  467. //                 
  468. //
  469. //----------------------------------------------------------------------
  470.  
  471. Boolean GetIsAboutWindow(WindowRef window)
  472. {
  473.     return (GetWindKind(window) == kAboutKind);
  474.     
  475. }
  476.  
  477.  
  478. //----------------------------------------------------------------------
  479. //
  480. //    GetContentRegion - 
  481. //                 
  482. //
  483. //----------------------------------------------------------------------
  484.  
  485. RgnHandle GetContentRegion(WindowRef window)
  486. {
  487.     return (((WindowPeek)window)->contRgn);
  488. }
  489.  
  490.  
  491. //----------------------------------------------------------------------
  492. //
  493. //    GetStructureRegion - 
  494. //                 
  495. //
  496. //----------------------------------------------------------------------
  497.  
  498. RgnHandle GetStructureRegion(WindowRef window)
  499. {
  500.     return (((WindowPeek)window)->strucRgn);
  501. }
  502.  
  503.